//
//  SinaService.m
//  OCDemo
//
//  Created by zhumuzhi on 2021/3/19.
//

#import "SinaService.h"


@interface SinaService (){
    
    NSURLSessionDataTask *_getAccessTokenTask;
    
    NSURLSessionDataTask *_getHomeTimelineTask;
    
    NSURLSessionDataTask *_getUsersShowTask;
    
}

@end

@implementation SinaService


static SinaService *_service = nil;

+ (instancetype)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _service = [[SinaService alloc] init];
    });
    return _service;
}

/// 获取微博授权Token
- (void)getWeiboAccessTokenSuccessBlock:(NetworkRequestSuccessBlock)successBlock
                           failureBlock:(NetworkRequestFailureBlock)failureBlock {
    
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    // 申请应用时分配的AppKey。
    params[@"client_id"] = kWeiboAppKey;
    // 申请应用时分配的AppSecret。
    params[@"client_secret"] = kWeiboAppSecret;
    // 请求的类型，填写authorization_code
    params[@"grant_type"] = @"authorization_code";
    // 调用authorize获得的code值。
    params[@"code"] = SafeStr([AppConfig shared].weiboCode);
    // 回调地址，需需与注册应用里的回调地址一致
    params[@"redirect_uri"] = kWeiboCallBackPage;
    
    NSLog(@"请求参数：%@", params);
    
    _getAccessTokenTask = [[NetWorkRequest sharedNetworkRequest] postRequest:urlString parameters:params successBlock:^(id  _Nullable returnData, RequestInfoModel * _Nullable requestInfoModel) {
        successBlock(returnData, requestInfoModel);
    } failureBlock:^(NSError * _Nullable error, RequestInfoModel * _Nullable requestInfoModel) {
        failureBlock(error, requestInfoModel);
    }];
}

- (void)cancelGetAccessToken {
    [[NetWorkRequest sharedNetworkRequest] cancelNetworkRequestWithTask:_getAccessTokenTask];
}





/// 获取用户信息
- (void)getUsersShowSuccessBlock:(NetworkRequestSuccessBlock)successBlock
                        failureBlock:(NetworkRequestFailureBlock)failureBlock {
    
    NSString *urlStr = @"https://api.weibo.com/2/users/show.json";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = [AppConfig shared].weiboAccess_Token;
    params[@"uid"] = [AppConfig shared].weiboUid;
    
    _getUsersShowTask = [[NetWorkRequest sharedNetworkRequest] getRequest:urlStr parameters:params successBlock:^(id  _Nullable returnData, RequestInfoModel * _Nullable requestInfoModel) {
        successBlock(returnData, requestInfoModel);
    } failureBlock:^(NSError * _Nullable error, RequestInfoModel * _Nullable requestInfoModel) {
        failureBlock(error, requestInfoModel);
    }];
    
}

- (void)cancelGetUsersShowTask {
    [[NetWorkRequest sharedNetworkRequest] cancelNetworkRequestWithTask:_getUsersShowTask];
}





/// 获取微博列表
- (void)getHomeTimelineSuccessBlock:(NetworkRequestSuccessBlock)successBlock
                        failureBlock:(NetworkRequestFailureBlock)failureBlock {
    
    NSString *urlString = @"https://api.weibo.com/2/statuses/home_timeline.json";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = [AppConfig shared].weiboAccess_Token;
    params[@"id"] = [AppConfig shared].weiboUid;
    params[@"count"] = @"3";
    params[@"page"] = @"1";
    
    _getHomeTimelineTask = [[NetWorkRequest sharedNetworkRequest] getRequest:urlString parameters:params successBlock:^(id  _Nullable returnData, RequestInfoModel * _Nullable requestInfoModel) {
        successBlock(returnData, requestInfoModel);
    } failureBlock:^(NSError * _Nullable error, RequestInfoModel * _Nullable requestInfoModel) {
        failureBlock(error, requestInfoModel);
    }];
    
}

- (void)cancelGetHomeTimelineTask { 
    [[NetWorkRequest sharedNetworkRequest] cancelNetworkRequestWithTask:_getHomeTimelineTask];
}



@end
